package com.decent.ejfadmin.configure.service;

import cn.decent.component.shiro.entity.Admin;
import cn.decent.component.shiro.service.AdminService;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.PutObjectRequest;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.PageBean;
import com.decent.ejfadmin.config.OssConfig;
import com.decent.ejfadmin.configure.dao.BannerConfigDao;
import com.decent.ejfadmin.configure.entity.UserPlatformBanner;
import com.decent.ejfadmin.configure.entity.dto.UserPlatformBannerDTO;
import com.decent.ejfadmin.utils.BaseToImage;
import com.decent.ejfadmin.utils.FileUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Objects;

/**
 * describe:会员系统banner
 *
 * @author 陈豆豆
 * @date 2019/08/10
 */
@Service
public class BannerConfigServiceImpl implements BannerConfigService {
    @Autowired
    private BannerConfigDao bannerConfigDao;
    @Autowired
    private AdminService adminService;
    @Autowired
    private OssConfig ossConfig;

    /**
     * banner列表
     *
     * @param userPlatformBanner
     * @param page
     * @return
     */
    @Override
    public PageBean<UserPlatformBanner> listBanner(UserPlatformBanner userPlatformBanner, Page page) {
        PageHelper.startPage(page.getBeginIndex(), page.getSize());
        return new PageBean<>(bannerConfigDao.listBanner(userPlatformBanner));
    }

    /**
     * 新增Banner
     *
     * @param userPlatformBanner
     * @return
     * @throws Exception
     */
    @Override
    public MessageBean addBanner(UserPlatformBannerDTO userPlatformBanner) throws Exception {
        Admin currentUser = adminService.getCurrentAdmin();
        userPlatformBanner.setRemark(currentUser.getName() + " 新增");
        UserPlatformBanner platformBanner = bannerConfigDao.getBannerBySort(userPlatformBanner.getSort());
        if (Objects.nonNull(platformBanner)) {
            return MessageBean.fail(userPlatformBanner.getSort() + "排序已存在");
        }
        pictureUpload(userPlatformBanner);
        if (bannerConfigDao.addBanner(userPlatformBanner) < 1) {
            return MessageBean.fail("添加失败");
        }
        return MessageBean.success();
    }

    /**
     * 修改Banner
     *
     * @param userPlatformBanner
     * @return
     * @throws Exception
     */
    @Override
    public MessageBean updateBanner(UserPlatformBannerDTO userPlatformBanner) throws Exception {
        pictureUpload(userPlatformBanner);
        Admin currentUser = adminService.getCurrentAdmin();
        userPlatformBanner.setRemark(currentUser.getName() + " 修改");
        if (bannerConfigDao.updateBanner(userPlatformBanner) < 1) {
            return MessageBean.fail("更新失败");
        }
        return MessageBean.success();
    }

    /**
     * 删除Banner
     *
     * @param id
     * @return
     */
    @Override
    public MessageBean deleteBanner(Integer id) {
        if (bannerConfigDao.deleteBanner(id) < 1) {
            return MessageBean.fail("删除失败");
        }
        return MessageBean.success();
    }

    /**
     * 图片上传到阿里云
     *
     * @param userPlatformBanner
     * @throws IOException
     */
    private void pictureUpload(UserPlatformBannerDTO userPlatformBanner) throws IOException {
        if(StringUtils.isBlank(userPlatformBanner.getImageString())){
            return;
        }
        String filePath = BaseToImage.generateImage(userPlatformBanner.getImageString());
        File tempFile = new File(filePath);
        String picKey = "bannerPicture/" + System.currentTimeMillis() + "/" + tempFile.getName() + ".pic";
        ossConfig.getOssClient().putObject(new PutObjectRequest(ossConfig.getBucketName(), picKey, tempFile));
        String url = ossConfig.getPicUrl(picKey, DateUtils.addYears(new Date(), 10));
        userPlatformBanner.setPictureUrl(url);
        userPlatformBanner.setPictureKey(picKey);
        userPlatformBanner.setImageBinary(new BASE64Decoder().decodeBuffer(userPlatformBanner.getImageString()));
    }
}
